// pages/recordDetail/index.js


import {
  ApiCancelOrder,
  ApiOrderDrug,
  ApiGetOrderListDrugById
} from '../../api/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    steps: [],
    isLoading: false,
    statusFormatter: [{
      label: '待支付',
      value: '1'
    }, {
      label: '已缴费',
      value: '2'
    }, {
      label: '已完成',
      value: '3'
    }, {
      label: '已取消',
      value: '4'
    }, {
      label: '已评价',
      value: '5'
    }, {
      label: '退款中',
      value: '6'
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    /*     this.getOrderListDrug(options.id) */

  },

  getOrderListDrug(id) {
    wx.showLoading({
      title: '加载中'
    })
    ApiGetOrderListDrugById(id).then(res => {
      let val = res.data
      //改变快递单物流信息
      if (val.express_detail) {
        val.express_detail.forEach(i => {
          i.text = '【' + i.acceptAddress + '】' + i.remark
          i.desc = i.acceptTotaltime
        })
      }
      //改变订单状态
      this.checkStatus(val)
      val.product = JSON.parse(val.product)
      if (val.status == '1') {
        val.statusLa = '待支付'
      } else {
        val.statusLa = '已支付'
      }
      //开始时间倒计时
      let a = this.computedCountDown(val.created_at)
      this.setData({
        countDown: a,
        detailData: val,
        steps: val.express_detail ? val.express_detail : [],
      })
      wx.hideLoading()
    })
  },


  opeData(e) {
    app.handleAdvice(e.currentTarget.dataset.v)

  },
  drugClick(e) {
    let order_id = e.currentTarget.dataset.v
    wx.navigateTo({
      url: `/pages/drugRecordDetailQrcode/index?order_id=${order_id}`,
    })

  },

  /*  onClose(e) {
     this.setData({
       showpopup: false
     })
   }, */






  formatter(e) {
    console.log(e)
  },
  onChangeEva(event) {
    this.setData({
      evaluationActive: event.detail,
    });
  },

  checkStatus(item) {
    this.data.statusFormatter.forEach(statusItem => {
      if (item.status == statusItem.value) {
        item.statusLabel = statusItem.label
      }
    })

  },
  computedCountDown(data) {
    const givenTime = new Date(data); // UTC时间表示
    const updatedTime = new Date(givenTime.getTime() + 15 * 60 * 1000);
    // 当前时间
    const currentTime = new Date();
    // 计算时间差（毫秒）
    const timeDifference = updatedTime - currentTime;
    return timeDifference
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  //取消预约
  cancelOrder() {
    let that = this
    wx.showModal({
      title: "是否取消预约该订单？",
      success(res) {
        if (res.confirm) {
          ApiCancelOrder(that.data.detailData.id).then((res) => {
            if (res.code == '200') {
              wx.showToast({
                title: "取消成功",
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 500);
            }

          })
        }

      }
    })


  },


  submitConsultation() {
    wx.navigateTo({
      url: `/pages/drugPay/index?order=${JSON.stringify(this.data.detailData)}`
    })
  },

  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOrderListDrug(this.data.id)
    /*   location.reload() */
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})